clear

set mem 100m
set matsize 4000
set more off

local suffix "test"   /*Name to be used for the output file for the specific group of industries*/


local evt_window = 10 /*Define the width of event dates to include as event day dummies*/
local estwindow = 180
local evtwindow = 30
local EVENT  "majorinitiative1summit" /*majorinitiative1, majorinitiative2, majorinitiative1summit, or majorinitiative2alt*/
local eveyear  "all" /*For majorinitiative1&2, estimate each event in 1996, 1999, 2005 separately, or all*/
local thintrade    = 0 /*0 (No thin trade) or 1*/
local controlevents = 1
local controlindustry = 1



use data_event_study_before_estimation_T_`estwindow'_`evtwindow'_thin`thintrade'_`EVENT', clear

keep if group=="finance" | group=="information" | group=="service" | group=="telcom" | group=="resource" /*Keeping resources and services*/
*Dummy for resources

gen resource =.
replace resource = 1 if group=="resource"
replace resource = 0 if group~="resource" & group~=""



if `thintrade' == 0 {
	replace R = R*100
	replace Rm = Rm*100
	replace R_group = R_group*100
}
if `thintrade'== 1 {
    replace Rindtt = Rindtt*100
    replace Rtt = Rtt*100
    replace Rmtt = Rmtt*100
}

*CREATE EVENT YEAR
gen tmp = year if event_trading_time==0
egen eventyear = min(tmp), by(panel_id)
drop tmp


if "`EVENT'"=="majorinitiative1" |"`EVENT'"=="majorinitiative1summit"  {
	drop if ticker=="GND" & eventyear==1996
	drop if event_time<-122 | event_time==. /*To be comparable to the portfolio approach*/
}
if "`EVENT'"=="majorinitiative2" | "`EVENT'"=="majorinitiative2alt" {
	drop if ticker=="ANG" & eventyear==1999
	drop if event_time<-122 | event_time==. /*To be comparable to the portfolio approach*/
}




if "`eveyear'" == "1996" {
	keep if eventyear==1996
}
if "`eveyear'" == "1999" {
	keep if eventyear==1999
}
if "`eveyear'" == "2005" {
	keep if eventyear==2005
}


if `controlindustry'==1 {
        if `thintrade'==0 {
            local industry "R_group"
        }
        else if `thintrade'==1 {
            local industry "Rindtt"
        }
}


local action ""
replace corporate_action = "0" if corporate_action==""


*Create event time dummies according to the desired window

gen year1= year(date)
replace year=0 if year==.

gen month = month(date)
replace month=0 if month==.

gen day1 = day
replace day1=0 if day1==.

gen Dmonth= mofd(date)
gen Dweek= wofd(date)




if `controlindustry'==1 {
	local industrycontrols "i.panel_id|`industry'"
}
else if `controlindustry'==0 {
	local industrycontrols ""
}

tab event_time if abs(event_time)<=`evt_window' & event_window==1, gen(Devent)
local neventdummies = 2*`evt_window'+1
forv i==1/`neventdummies' {
		replace Devent`i' = 0 if Devent`i' == .
}



*Interact event time dummies with resource dummy

foreach v of varlist Devent* {
	gen resourceX`v' = resource*`v'
}

pause

drop if event_time > `evt_window'

/*Robust check of the length of the estimation window*/
su event_time
local mintime = r(min)
local maxtime = -`evt_window' - 50
local robust = `maxtime'-`mintime'+ 1 /*Case for baseline estimation window only*/


matrix A1 = J(`neventdummies',floor((`maxtime'-`mintime')/`robust'+1)*6,.)		

*=======================================================================================================
*REGRESSIONS with all coefficients panel_id specific (except event time dummies)
*=======================================================================================================

*I. NO-THIN TRADE ISSUES
*-------------------------------------------------------------------------------------------------------

if `thintrade'== 0 {
*================================================================================
*1. Baseline specification (i.e., no addiitonal time dummy variables)
*================================================================================
local m = 0

forv n==`mintime'(`robust')`maxtime' {

	xi: reg  R  i.panel_id*Rm  `industrycontrols'  i.panel_id*i.corporate_action  Devent* resource* if event_time>=`n', cluster(date) r

	local diff10 = (_b[Devent11]+_b[Devent12]+_b[Devent13]+_b[Devent14]+_b[Devent15]+_b[Devent16]+_b[Devent17]+_b[Devent18]+_b[Devent19]+_b[Devent20]+_b[Devent21])/11 ///
			  - (_b[Devent1]+_b[Devent2]+_b[Devent3]+_b[Devent4]+_b[Devent5]+_b[Devent6]+_b[Devent7]+_b[Devent8]+_b[Devent9]+_b[Devent10])/10
	test (Devent1+Devent2+Devent3+Devent4+Devent5+Devent6+Devent7+Devent8+Devent9+Devent10)/10 ///
		= (Devent11+Devent12+Devent13+Devent14+Devent15+Devent16+Devent17+Devent18+Devent19+Devent20+Devent21)/11
	local F1 = r(F)
	local p1 = r(p)

	local diff5 = (_b[Devent11]+_b[Devent12]+_b[Devent13]+_b[Devent14]+_b[Devent15]+_b[Devent16])/6 ///
			  - (_b[Devent6]+_b[Devent7]+_b[Devent8]+_b[Devent9]+_b[Devent10])/5
	test (Devent6+Devent7+Devent8+Devent9+Devent10)/5 = (Devent11+Devent12+Devent13+Devent14+Devent15+Devent16)/6
	local F2 = r(F)
	local p2 = r(p)

	local diff3 = (_b[Devent11]+_b[Devent12]+_b[Devent13]+_b[Devent14])/4 - (_b[Devent8]+_b[Devent9]+_b[Devent10])/3
	test (Devent8+Devent9+Devent10)/3 = (Devent11+Devent12+Devent13+Devent14)/4

	
	if `m'==0 {
		outreg2 Devent* resource* using Regression_Approach_Final_baseline_`eveyear'_`EVENT'_res, se  aster(coef) nolabel ctitle(`n')  excel ///
		addstat(-10to-1vs0to10, `diff10', F test, `F1', Prob>F, `p1', -5to-1vs0to5, `diff5', F test, `F2', Prob>F, `p2', -3to-1vs0to3, `diff3', F test,  r(F), Prob>F, r(p)) ///
		 replace
	}
	else {
		outreg2 Devent* resource* using Regression_Approach_Final_baseline_`eveyear'_`EVENT'_res, se  aster(coef) nolabel ctitle(`n')  excel ///
		addstat(-10to-1vs0to10, `diff10', F test, `F1', Prob>F, `p1', -5to-1vs0to5, `diff5', F test, `F2', Prob>F, `p2', -3to-1vs0to3, `diff3', F test,  r(F), Prob>F, r(p)) ///
		append
	}
	

	*Building CAR and their standard errors

	forv i==1/`neventdummies' {
		if `i'==1 {
			matrix A1[`i',`m'+1] = _b[Devent1]
			matrix A1[`i',`m'+2] = _se[Devent1]
			matrix A1[`i',`m'+3] = 2*ttail(e(df_r),abs(_b[Devent1]/_se[Devent1]))
		}
		else {
			local exp "Devent1"

			forv j == 2/`i' {
				local exp "`exp' + Devent`j'"
			}

			qui lincom `exp'
			matrix A1[`i',`m'+1] = r(estimate)
			matrix A1[`i',`m'+2] = r(se)
			matrix A1[`i',`m'+3] = 2*ttail(r(df),abs(r(estimate)/r(se)))
		}
	}
	
	*Storing the differentials
	
	forv i==1/`neventdummies' {
			if `i'==1 {
				matrix A1[`i',`m'+4] = _b[resourceXDevent1]
				matrix A1[`i',`m'+5] = _se[resourceXDevent1]
				matrix A1[`i',`m'+6] = 2*ttail(e(df_r),abs(_b[resourceXDevent1]/_se[resourceXDevent1]))
			}
			else {
				local exp "resourceXDevent1"
	
				forv j == 2/`i' {
					local exp "`exp' + resourceXDevent`j'"
				}
	
				qui lincom `exp'
				matrix A1[`i',`m'+4] = r(estimate)
				matrix A1[`i',`m'+5] = r(se)
				matrix A1[`i',`m'+6] = 2*ttail(r(df),abs(r(estimate)/r(se)))
			}
	}

local m = `m'+6

}


}



*II. THIN TRADE ISSUES
*-------------------------------------------------------------------------------------------------------


if `thintrade'== 1 {

*================================================================================
*1. Baseline specification (i.e., no addiitonal time dummy variables)
*================================================================================
local m = 0

forv n==`mintime'(`robust')`maxtime' {

	xi: reg  Rtt  i.panel_id*ntt  i.panel_id|Rmtt  `industrycontrols'  i.panel_id*i.corporate_action  Devent* resource* if event_time>=`n', cluster(date) r

	local diff10 = (_b[Devent11]+_b[Devent12]+_b[Devent13]+_b[Devent14]+_b[Devent15]+_b[Devent16]+_b[Devent17]+_b[Devent18]+_b[Devent19]+_b[Devent20]+_b[Devent21])/11 ///
			  - (_b[Devent1]+_b[Devent2]+_b[Devent3]+_b[Devent4]+_b[Devent5]+_b[Devent6]+_b[Devent7]+_b[Devent8]+_b[Devent9]+_b[Devent10])/10
	test (Devent1+Devent2+Devent3+Devent4+Devent5+Devent6+Devent7+Devent8+Devent9+Devent10)/10 ///
		= (Devent11+Devent12+Devent13+Devent14+Devent15+Devent16+Devent17+Devent18+Devent19+Devent20+Devent21)/11
	local F1 = r(F)
	local p1 = r(p)

	local diff5 = (_b[Devent11]+_b[Devent12]+_b[Devent13]+_b[Devent14]+_b[Devent15]+_b[Devent16])/6 ///
			  - (_b[Devent6]+_b[Devent7]+_b[Devent8]+_b[Devent9]+_b[Devent10])/5
	test (Devent6+Devent7+Devent8+Devent9+Devent10)/5 = (Devent11+Devent12+Devent13+Devent14+Devent15+Devent16)/6
	local F2 = r(F)
	local p2 = r(p)

	local diff3 = (_b[Devent11]+_b[Devent12]+_b[Devent13]+_b[Devent14])/4 - (_b[Devent8]+_b[Devent9]+_b[Devent10])/3
	test (Devent8+Devent9+Devent10)/3 = (Devent11+Devent12+Devent13+Devent14)/4

	if `m'==0 {
		outreg2 Devent* using Regression_Approach_Final_baseline_thin_`eveyear'_`EVENT', se  aster(coef) nolabel ctitle(`n')  excel ///
		addstat(-10to-1vs0to10, `diff10', F test, `F1', Prob>F, `p1', -5to-1vs0to5, `diff5', F test, `F2', Prob>F, `p2', -3to-1vs0to3, `diff3', F test,  r(F), Prob>F, r(p)) ///
		adec(5, 3, 3, 5, 3, 3, 5, 3, 3) replace
	}
	else {
		outreg2 Devent* using Regression_Approach_Final_baseline_thin_`eveyear'_`EVENT', se  aster(coef) nolabel ctitle(`n') excel ///
		addstat(-10to-1vs0to10, `diff10', F test, `F1', Prob>F, `p1', -5to-1vs0to5, `diff5', F test, `F2', Prob>F, `p2', -3to-1vs0to3, `diff3', F test,  r(F), Prob>F, r(p)) ///
		adec(5, 3, 3, 5, 3, 3, 5, 3, 3) append
	}
	
	
	*Building CAR and their standard errors

	forv i==1/`neventdummies' {
		if `i'==1 {
			matrix A1[`i',`m'+1] = _b[Devent1]
			matrix A1[`i',`m'+2] = _se[Devent1]
			matrix A1[`i',`m'+3] = 2*ttail(e(df_r),abs(_b[Devent1]/_se[Devent1]))
		}
		else {
			local exp "Devent1"

			forv j == 2/`i' {
				local exp "`exp' + Devent`j'"
			}

			qui lincom `exp'
			matrix A1[`i',`m'+1] = r(estimate)
			matrix A1[`i',`m'+2] = r(se)
			matrix A1[`i',`m'+3] = 2*ttail(r(df),abs(r(estimate)/r(se)))
		}
	}

	forv i==1/`neventdummies' {
			if `i'==1 {
				matrix A1[`i',`m'+4] = _b[resourceXDevent1]
				matrix A1[`i',`m'+5] = _se[resourceXDevent1]
				matrix A1[`i',`m'+6] = 2*ttail(e(df_r),abs(_b[resourceXDevent1]/_se[resourceXDevent1]))
			}
			else {
				local exp "resourceXDevent1"
	
				forv j == 2/`i' {
					local exp "`exp' + resourceXDevent`j'"
				}
	
				qui lincom `exp'
				matrix A1[`i',`m'+4] = r(estimate)
				matrix A1[`i',`m'+5] = r(se)
				matrix A1[`i',`m'+6] = 2*ttail(r(df),abs(r(estimate)/r(se)))
			}
	}

	local m = `m'+6

}


}


mat list A1


egen id = group(panel_id)
keep if id==1
keep if event_time>=-10 & event_time<=10
keep event_time
svmat A1, names( col )


local max = floor((`maxtime'-`mintime')/`robust'+1)

forv i==1/`max' {
	local m1 = ( `i'-1)*6+1
	local m2 = ( `i'-1)*6+2
	local m3 = ( `i'-1)*6+3
	local m4 = ( `i'-1)*6+4
	local m5 = ( `i'-1)*6+5
	local m6 = ( `i'-1)*6+6

	rename c`m1' car0`i'
	rename c`m2' se0`i'
	rename c`m3' p0`i'
	
	gen star0`i' = car0`i'  if p0`i' <=0.1
	
	rename c`m4' car1`i'
	rename c`m5' se1`i'
	rename c`m6' p1`i'

	gen star1`i' = car1`i'  if p1`i' <=0.1
}

di `max'

sort event_time

if `thintrade'== 0 {
	saveold Regression_Approach_CAR_baseline_`eveyear'_`EVENT'_`suffix'_diff, replace
}
if `thintrade'== 1 {
	saveold Regression_Approach_CAR_baseline_thin_`eveyear'_`EVENT'_`suffix'_diff, replace
}



*****************************************************************************
*To draw figures of the CAR evolution 						    *
*****************************************************************************
forv n=1/21 {
	 replace event_time = `n'-11 in `n'
}


gen ciu1 = car11 + se11*1.64
gen cil1 = car11 - se11*1.64

twoway (line car11 event_time, lwidth(thick)) ///
(line ciu1 event_time, lpattern(shortdash) lwidth(thin) lcolor(red)) ///
(line cil1 event_time, lpattern(shortdash) lwidth(thin) lcolor(red)) ///
, yline(0, lwidth(vvthin)) /*yscale(range(0 0.01))*/ ytitle(Cumulative abnormal return, margin(medium)) xtitle(Event time, margin(medium)) legend(off)

gr save Figure_10_C, replace
